package monster.com.cvh.adapter;

import android.app.DialogFragment;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import monster.com.cvh.R;
import monster.com.cvh.listener.OnPublishListener;


public class DetailCommentDialog extends DialogFragment {
    private Button mBtnPulish;
    private Button mBtnCancel;
    private OnPublishListener mPublishListener;
    private String mTypeKey;
    private String mType;
    private String mTypeValue;

    public static DetailCommentDialog newInstance(String typeKey, String type, String typeValue) {
        DetailCommentDialog dialog = new DetailCommentDialog();
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putString("typeKey", typeKey);
        args.putString("typeValue", typeValue);
        Log.e("JJY", "type: " + type + ";typeKey:" + typeKey + ":typeValue:" + typeValue);
        dialog.setArguments(args);
        return dialog;
    }

    public void setPublishListener(OnPublishListener publishListener) {
        mPublishListener = publishListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM; // 显示在底部
        params.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度填充满屏
        params.height = 400 ;
        window.setAttributes(params);
        // 这里用透明颜色替换掉系统自带背景
        int color = ContextCompat.getColor(getActivity(), android.R.color.transparent);
        window.setBackgroundDrawable(new ColorDrawable(color));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle args = getArguments();
        mType = args.getString("type");
        mTypeKey = args.getString("typeKey");
        mTypeValue = args.getString("typeValue");
        Log.e("JJY", "type: " + mType + ";typeKey:" + mTypeKey + ":typeValue:" + mTypeValue);
        // 不显示标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        final View view = inflater.inflate(R.layout.dialog_forum_comment, container, false);
        mBtnPulish = (Button) view.findViewById(R.id.btn_forum_publish);
        mBtnCancel = (Button) view.findViewById(R.id.btn_forum_cancel);
        final EditText edtComment = (EditText) view.findViewById(R.id.edt_comment);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //让编辑框弹出来，并显示对谁进行评论
                edtComment.setFocusable(true);
                edtComment.setFocusableInTouchMode(true);
                edtComment.requestFocus();
                //打开软键盘
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 200);

        startUpAnimation(view);

        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDownAnimation(view);
            }
        });
        mBtnPulish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("JJY", "onClick: " + edtComment.getText().toString());
//                OkGo.post("http://api.seebtc.io/emlog/comment")
//                        .params("token", getActivity().getApplication().getSharedPreferences("USER_TOKEN", Activity.MODE_PRIVATE).getString("token", ""))
//                        .params("content", edtComment.getText().toString())
//                        .params("type", mType)
//                        .params(mTypeKey, mTypeValue)
//                        .execute(new StringCallback() {
//                            @Override
//                            public void onSuccess(String s, Call call, Response response) {
//                                ResultBean resultBean = new Gson().fromJson(s, ResultBean.class);
//                                if (resultBean.getStatus() == 1 && mPublishListener != null) {
//                                    mPublishListener.refreshData();
//                                    startDownAnimation(view);
//                                } else {
//                                    Toast.makeText(getActivity().getApplicationContext(), "发布失败,请重试！status:" + resultBean.getStatus(), Toast.LENGTH_SHORT).show();
//                                    Log.e("JJY",
//                                            "token: " + getActivity().getApplication().getSharedPreferences("USER_TOKEN", Activity.MODE_PRIVATE).getString("token", "")
//                                                    + "title：" + edtComment.getText().toString()
//                                                    + "type：" + mType
//                                                    + "mTypeKey：" + mTypeKey
//                                                    + "mTypeValue：" + mTypeValue
//                                    );
//                                }
//                            }
//                        });
            }
        });
        return view;
    }

    private void startUpAnimation(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);
    }

    private void startDownAnimation(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(slide);
    }
}